package com.xhui.vimarket.zkplus.zul.attr.mod.impl;t java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.directwebremoting.util.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Div;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Span;

import com.xhui.crm.common.util.numeric.NumericUtil;
import com.xhui.crm.common.util.string.StrUtil;
import com.xhui.crm.common.vo.NodeVo;
import com.xhui.crm.common.vo.RetVO;com.xhui.vimarket.zkplusui.vimarket.zkplus.util.com.xhui.vimarket.zkplusrt com.xhui.vimarket.zkpcom.xhui.vimarket.zkplusil;
import com.xhui.vimarkcom.xhui.vimarket.zkplusttr.AttrExt;
import com.xhui.vimcom.xhui.vimarket.zkplusl.attr.bean.AttrInst;
import comcom.xhui.vimarket.zkpluszkplus.zul.attr.bean.AttrTmpl;com.xhui.vimarket.zkplusi.vimarket.zkplus.zul.attr.bean.ValiVo;
com.xhui.vimarket.zkplus.vimarket.zkplus.zul.attr.mod.AttrCompConstants;
import com.xhui.vimacom.xhui.vimarket.zkplus.attr.mod.AttrCompUtil;

/**
 * @版权：福富软件 版权所有 (c) 2010
 * @文件：com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrSelectlistComp.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-20
 * @功能说明：属性控件接口
 * @修改记录： =============================================================<br>
 *        日期:2010-12-20 创建
 *        =============================================================<br>
 */
public final class AttrMultiCheckboxComp extends AttrBaseComp {
    
    /**
     * 实例.
     */
    private static AttrMultiCheckboxComp inst   = new AttrMultiCheckboxComp();
    /**
     * 日志.
     */
    private static Logger                logger = Logger.getLogger(AttrMultiCheckboxComp.class);
    
    /**
     * 构造函数. 
     */
    private AttrMultiCheckboxComp() {
    }
    
    /**
     * 获取实例.
     * 
     * @return AttrBaseComp
     */
    public static AttrMultiCheckboxComp instance() {
        return AttrMultiCheckboxComp.inst;
    }
    
    /**
     * 生成组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param comp 显示容器组件
     */
    @SuppressWarnings("deprecation")
    @Override
    public void create(final AttrExt attrExt, final AttrTmpl attrTmpl, final Component comp) {
        super.create(attrExt, attrTmpl, comp);
        // panel
        final Hbox hbox = new Hbox();
        hbox.setAttribute(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
        hbox.setAttribute(AttrBaseComp.COMP_ATTRID, attrTmpl.getAttrId());
        hbox.setAttribute(AttrBaseComp.COMP_JAVACODE, attrTmpl.getJavaCode());
        hbox.setAttribute(AttrBaseComp.COMP_CANTNULL, AttrCompUtil.getStrCantNull(attrTmpl
            .isCantNull()));
        hbox.setAttribute(AttrBaseComp.COMP_COMP_NAME, attrTmpl.getName());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_LENGTH, attrTmpl.getValueLength());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_FROM, attrTmpl.getValueFrom());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_TO, attrTmpl.getValueTo());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_REX, attrTmpl.getValidExpr());
        hbox.setWidth("100%");
        hbox.setWidths(this.getWidthsLCT(attrTmpl));
        final Div labDiv = new Div();
        final Label label = new Label(attrTmpl.getName());
        label.setSclass("zp-lt-label");
        // info
        final Span infDiv = this.createRulebar(attrExt, hbox, attrTmpl);
        // add to zul
        hbox.setParent(comp);
        labDiv.setParent(hbox);
        label.setParent(labDiv);
        infDiv.setParent(labDiv);
        // listbox.setParent(hbox);
        // final Listbox listbox = new Listbox();
//        final String sListboxWithComposer = attrExt.getAttrAda().getAttrTmplCompStr(
//            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null);
        final Div chboxDiv = new Div();
        chboxDiv.setParent(hbox);
        chboxDiv.setWidth(AttrBaseComp.WIDTH_SELECT);
        if (attrTmpl.isReadOnly()) {
        }
        if (attrTmpl.getHint() != null) {
            hbox.setTooltiptext(attrTmpl.getHint());
        }
        if (attrTmpl.getValueList() != null && attrTmpl.getValueList().size() > 0) {
            for (int i = 0; i < attrTmpl.getValueList().size(); i++) {
                final NodeVo item = attrTmpl.getValueList().get(i);
                final Checkbox chbox = new Checkbox();
                chbox.setLabel(item.getName());
                chbox.setValue(item.getId());
                chbox.setParent(chboxDiv);
            }
        }
        // toolbox
        this.createToolbox(hbox, attrExt.isToolboxVisible(), attrTmpl);
        
        // comp log
        if (AttrMultiCheckboxComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("<hbox width=\"100%\" widths=\"40%,60%\" >");
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_ATTRID, "" + attrTmpl.getAttrId());
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_JAVACODE, "" + attrTmpl.getJavaCode());
            attrExt.appendInnerComp("<label value=\"");
            attrExt.appendInnerComp(attrTmpl.getName());
            attrExt.appendInnerComp("\" />");
            attrExt.appendInnerComp("<listbox ");
            if (attrTmpl.isReadOnly()) {
                attrExt.appendInnerComp("readonly=\"true\"");
            }
            attrExt.appendInnerComp(">");
            if (attrTmpl.getValueList() != null && attrTmpl.getValueList().size() > 0) {
                for (int i = 0; i < attrTmpl.getValueList().size(); i++) {
                    final NodeVo item = attrTmpl.getValueList().get(i);
                    attrExt.appendInnerComp("<listitem value=\"");
                    attrExt.appendInnerComp(item.getId());
                    attrExt.appendInnerComp("\" label=\"");
                    attrExt.appendInnerComp(item.getName());
                    attrExt.appendInnerComp("\" />");
                }
            }
            attrExt.appendInnerComp("</listbox></hbox>");
        }
        
        super.afterCreate(attrExt, attrTmpl, comp);
    }
    
    /**
     * 设置值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param attrInstList 属性实例
     */
    @Override
    public void setValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList,final Map<String, Integer> attrViewMap) { 
        super.setValue(attrExt, comp, attrInstList,attrViewMap);
        //列表
        final Component compChbDiv = getCheckboxDiv(comp);
        if (compChbDiv != null) {
            final long theAttrId = NumUtils
                .toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID));
            final ArrayList<AttrInst> theAttrList = AttrBaseComp.getAttrInstListById(attrInstList,
                theAttrId);
            if (theAttrList.size() > 0) {
                if (theAttrList.get(0).getSubAttrInsts() != null
                    && theAttrList.get(0).getSubAttrInsts().size() > 0) {
                    for (AttrInst attrInst : theAttrList.get(0).getSubAttrInsts()) {
                        AttrMultiCheckboxComp.selectByCodeValue((Div) compChbDiv, attrInst
                            .getParam1());
                    }
                } else {
                    AttrMultiCheckboxComp.selectByCodeValue((Div) compChbDiv, theAttrList.get(0)
                        .getParam1());
                }
                // javacode回填
                AttrCompUtil.copyTmplInfo(theAttrList.get(0), comp);
            }
        }
    }
    
    /**
     * 获取值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getValue(final AttrExt attrExt, final Component comp) {
        super.getValue(attrExt, comp);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        // 取列表
        final Component compListbox = getCheckboxDiv(comp);
        if (compListbox != null && compListbox.getChildren() != null) {
            List<AttrInst> paramAttrInsts = new ArrayList<AttrInst>();
            for (Object chboxComp : compListbox.getChildren()) {
                Checkbox chbox = (Checkbox) chboxComp;
                if (chbox.isChecked()) {
                    final AttrInst attrInst = attrExt.newAttrInst();
                    attrInst.setAttrId(NumUtils.toLong(""
                        + comp.getAttribute(AttrBaseComp.COMP_ATTRID)));
                    attrInst.setJavaCode(StrUtil.strnull(comp
                        .getAttribute(AttrBaseComp.COMP_JAVACODE)));
                    attrInst.setParam1(StrUtil.strnull(chbox.getValue()));
                    attrInst.setOp(this.getToolbarOp(comp));
                    paramAttrInsts.add(attrInst);
                }
            }
            if (paramAttrInsts.size() == 1) { // 单值，放在param1中保持
                final AttrInst attrInst = attrExt.newAttrInst();
                attrInst.setAttrId(NumUtils
                    .toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID)));
                attrInst
                    .setJavaCode(StrUtil.strnull(comp.getAttribute(AttrBaseComp.COMP_JAVACODE)));
                attrInst.setParam1(StrUtil.strnull(paramAttrInsts.get(0).getParam1()));
                attrInst.setOp(this.getToolbarOp(comp));
                retList.add(attrInst);
            } else if (paramAttrInsts.size() > 1) { // 多值，放在下级节点保存
                final AttrInst attrInst = attrExt.newAttrInst();
                attrInst.setAttrId(NumUtils
                    .toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID)));
                attrInst
                    .setJavaCode(StrUtil.strnull(comp.getAttribute(AttrBaseComp.COMP_JAVACODE)));
                attrInst.setSubAttrInsts(paramAttrInsts);
                attrInst.setOp(this.getToolbarOp(comp));
                retList.add(attrInst);
            }
        com.xhui.vimarket.zkplusrn retList;
        
    }

    /**
    com.xhui.vimarket.zkplus}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp#signRuleHint(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2012-2-16 wuyx
     */
    @Override
    public ArrayList<ValiVo> signRuleHint(AttrExt attrExt, Component comp) {
        super.signRuleHint(attrExt, comp);
        if (AttrCompUtil.isCantNull(comp)) { // 必填
            ArrayList<AttrInst> valList = this.getValue(attrExt, comp);
            if (valList == null || valList.size() < 1) {
                ValiVo retVo = new ValiVo();
                ArrayList<ValiVo> retVoList = new ArrayList<ValiVo>();
                retVoList.add(retVo);
                return retVoList;
            }
        }
        return null;
    }
    
    /**
     * 取空值属性实例.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 模板
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final AttrTmpl attrTmpl) {
        super.getNullValue(attrExt, attrTmpl);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        final AttrInst attrInst = attrExt.newAttrInst();
        attrInst.setAttrId(attrTmpl.getAttrId());
        attrInst.setJavaCode(attrTmpl.getJavaCode());
        retList.add(attrInst);
        return retList;
    }
    
    /**
     * selectByCodeValue.
     * @param chbDiv Div
     * @param value String
     */
    public static void selectByCodeValue(final Div chbDiv, final String value) {
        if (chbDiv == null) {
            throw new IllegalArgumentException("listbox must not null!");
        }
        if (StrUtil.isNullOrEmpty(value)) {
            return;
        }
        for (final Object object : chbDiv.getChildren()) {
            final Checkbox chb = (Checkbox) object;
            final String itemValue = chb.getValue();
            if (itemValue != null && itemValue.equals(value)) {
                chb.setChecked(true);com.xhui.vimarket.zkplus  return;
            }
     com.xhui.vimarket.zkplus  
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp#getComp(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2011-1-24 wuyx
     */
    @Override
    public List<Component> getComp(final AttrExt attrExt, final Component comp, final long attrId) {
        final List<Component> retList = new ArrayList<Component>();
        // 取列表
        final Object oAttrId = comp.getAttribute(AttrBaseComp.COMP_ATTRID);
        if (oAttrId == null || "".equals(oAttrId)) {
            return null;
        } else if (attrId == NumericUtil.toLong(oAttrId + "")) {
            retList.add(comp);
        }
        return retList;
    }
    
    /**
     * getListbox.
     * 
     * @param root Component
     * @return Listbox
     */
    public static Div getCheckboxDiv(Component root) {
        // 取输入框
        // hbox // Listbox(1) 
        return (Div) root.getFirstChild().getNextSibling();
    }
    
    /**
     * 设置为不允许修改.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param tflag 设置值，true/false
     */
    @Override
    public void setDisabled(AttrExt attrExt, Component comp, boolean tflag) {
        super.setDisabled(attrExt, comp, tflag);
        ZkUiUtil.setDisabled(comp, tflag);
    }
    
    @Override
    public List<RetVO> validate(AttrExt attrExt, Component comp) {
        if (AttrCompUtil.isCantNull(comp)) { // 需限制不可空
            ArrayList<AttrInst> valList = this.getValue(attrExt, comp);
            if (valList == null || valList.size() < 1) { // 没有值
                return AttrCompUtil.getRetVoList(AttrCompConstants.RET_ERROR, AttrCompUtil
                    .getCompName(comp)
                    + AttrCompConstants.STRING_CANT_NULL, comp);
            }
            return null; // 通过
        }
        return null; // 通过
    }
}
